<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="1300px">
        <el-form :model="queryParams" ref="queryForm" v-show="showSearch" size="small" inline label-width="90px">
            <el-form-item label="广告账户" prop="advertiser_id">
                <el-input v-model.trim="queryParams.advertiser_id" placeholder="请输入广告账户" clearable size="small" style="width: 200px" @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="时间" prop="customDate">
                <el-radio-group v-model="queryParams.customDate" @change="changeCustomDate">
                    <el-radio-button :label="1">今天</el-radio-button>
                    <el-radio-button :label="2">昨天</el-radio-button>
                    <el-radio-button :label="3">近7天</el-radio-button>
                </el-radio-group>
            </el-form-item>

            <el-form-item label="创建时间" prop="createtime">
                <el-date-picker v-model="queryParams.createtime" format="yyyy-MM-dd" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" style="width: 240px"></el-date-picker>
            </el-form-item>
            <el-form-item label=" ">
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="warning" size="small" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
            </el-col>
            <!-- <right-toolbar size="small" :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
        </el-row>
        <el-row :gutter="10" class="total-warp">
            <el-col :md="6" :sm="24">
                <el-card shadow="hover" class="text-large chart-card">
                    <div class="chart-card-header text-mini" slot="header">{{ platform_name }}</div>
                    <div class="chart-card-content">
                        <span class="content-title">{{ spends_Total }}</span>
                    </div>
                </el-card>
            </el-col>
        </el-row>
        <el-tabs v-model="queryParams.platform_id" type="border-card" @tab-click="handleQuery">
            <el-tab-pane v-for="item in platformList" :key="item.id" :label="item.title" :name="item.id.toString()"></el-tab-pane>
        </el-tabs>
        <div class="table-wrap">
            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column label="广告平台" prop="platform_id" min-width="" show-overflow-tooltip>
                    <template slot-scope="scope">
                        {{ scope.row.platform_id | formatLabelByValueFuzzy(platformList, { label: 'title', value: 'id' }) }}
                    </template>
                </el-table-column>
                <el-table-column label="店铺" prop="shop_name" min-width="" show-overflow-tooltip></el-table-column>
                <el-table-column label="运营" prop="admin_name" min-width="" show-overflow-tooltip></el-table-column>
                <el-table-column label="广告账户" prop="advertiser_id" min-width="160" show-overflow-tooltip></el-table-column>
                <el-table-column label="广告名称" prop="advertiser_id" min-width="160" show-overflow-tooltip></el-table-column>
                <el-table-column label="花费" prop="spend" min-width="" show-overflow-tooltip></el-table-column>
                <el-table-column label="点击量" prop="impressions" min-width="" show-overflow-tooltip></el-table-column>
                <!-- <el-table-column label="操作" width="160" fixed="right">
                    <template slot-scope="scope">
                        <el-button v-if="scope.row.ConfirmStatus == '等待确认'" type="text" icon="el-icon-edit" @click="advertisementConfirm(scope.row)" style="padding: 2px 0">确认校对</el-button>
                    </template>
                </el-table-column> -->
            </el-table>
        </div>

        <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList" />
    </el-dialog>
</template>

<script>
import { queryAdvplatformList, advertisementConfirm } from '@/api/advertisement'
import { getAdList, exportCost } from '@/api/orderReportDataScreen'
import { parseTime } from '@/utils/quickadmin'
export default {
    name: 'ReportCostDialog',
    components: {},
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            loading: false,
            download: false,
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            platformList: [],
            queryParams: {
                advertiser_id: undefined,
                type_name: undefined,
                platform_id: this.params.platform_id || '1',
                customDate: 2,
                createtime: [],
            },
            pageInfo: {
                page: 1,
                limit: 10,
            },
            platform_name: '',
            spends_Total: '',
        }
    },
    computed: {
        title() {
            return `运营花费列表`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    async mounted() {
        this.loading = true
        this.loading = true
        try {
            const [PlatformData] = await Promise.all([queryAdvplatformList()])
            this.platformList = PlatformData?.data?.list ?? []
        } catch (error) {
            this.loading = false
            console.log(error)
        }
        this.getList()
    },
    methods: {
        changeCustomDate() {
            this.queryParams.createtime = []
            this.handleQuery()
        },
        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
        advertisementConfirm(row) {
            this.$confirm('是否确认校对', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                advertisementConfirm({ id: row.id }).then(() => {
                    this.$message.success('操作成功')
                    this.handleQuery()
                })
            })
        },
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.createtime?.length == 2) {
                qyparams.createtime = [parseTime(qyparams.createtime[0], 'y-m-d'), parseTime(qyparams.createtime[1], 'y-m-d')]
            }
            getAdList(qyparams)
                .then(res => {
                    this.tableData = res.data?.list ?? []
                    this.total = res.data?.total ?? 0
                    this.platform_name = res.data?.platform_name ?? ''
                    this.spends_Total = res.data?.spends_Total ?? 0
                    // this.queryParams.platform_id = res.data?.platform_id ?? '1'
                })
                .finally(() => {
                    this.loading = false
                })
        },
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            exportCost(qyparams)
                .then(res => {
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    console.log(err)
                    this.download = false
                })
        },
    },
}
</script>

<style scoped>
::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.el-tabs--border-card {
    box-shadow: none;
    border-bottom: 0;
}

::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.table-wrap {
    padding: 15px;
    border-bottom: 1px solid #dfe4ed;
    border-left: 1px solid #dfe4ed;
    border-right: 1px solid #dfe4ed;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
}
.total-warp {
    font-size: 18px;
    font-weight: bold;
    padding: 8px 0 16px 0;
}
</style>
